package ibms_rcp.pay.parts;

import ibms_rcp.common.ChooseCompanyDialog;
import ibms_rcp.dao.BillManager;
import ibms_rcp.dao.EstateManager;
import ibms_rcp.dao.PayManager;
import ibms_rcp.model.Building;
import ibms_rcp.model.Company;
import ibms_rcp.model.Floor;
import ibms_rcp.model.Room;
import ibms_rcp.model.SeqGenereter;
import ibms_rcp.tableViewer.TableContentProvider;
import ibms_rcp.tableViewer.TableLabelProvider;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Table;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.window.Window;

/**
 * 消费结算界面
 * 
 * @author diamond-wang
 * 
 */
public class AddPart {
	/* 公司id */
	private Text idText;
	/* 公司编号 */
	private Text nameText;
	// 选择公司
	private Button chooseCompanyButton;
	// 结算项目
	private Combo itemCombo;
	// 楼栋
	private ComboViewer buildingComboViewer;
	private Combo buildingCombo;
	// 楼层
	private ComboViewer floorComboViewer;
	private Combo floorCombo;
	// 房间
	private ComboViewer roomComboViewer;
	private Combo roomCombo;
	// 查询
	private Button queryButton;
	// 付费
	private Button payButton;

	private TableViewer tableViewer;
	private Table table;

	@Inject
	public AddPart(Composite parent) {
	}

	@PostConstruct
	public void postConstruct(final Composite parent) {
		GridLayout layout = new GridLayout(1, false);
		layout.marginWidth = 10;
		layout.marginHeight = 10;
		parent.setLayout(layout);

		Label title = new Label(parent, SWT.NONE);
		title.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.BOLD));
		title.setText("消费结算");

		Composite payComposite = new Composite(parent, SWT.BORDER);
		payComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		payComposite.setLayout(new GridLayout(6, false));

		// 公司名称
		Label nameLabel = new Label(payComposite, SWT.RIGHT);
		nameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		nameLabel.setText("公司名称：");
		nameText = new Text(payComposite, SWT.BORDER);
		nameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				2, 1));
		nameText.setEditable(false);
		
		// 选择公司
		chooseCompanyButton = new Button(payComposite, SWT.NONE);
		chooseCompanyButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER,
				true, false, 1, 1));
		chooseCompanyButton.setText("选择公司");
		//点击弹出选择公司窗口
		chooseCompanyButton.addMouseListener(new MouseAdapter(){
			public void mouseUp(MouseEvent e) {
				ChooseCompanyDialog companyDialog = new ChooseCompanyDialog(parent.getShell());
				companyDialog.create();
				if (companyDialog.open() == Window.OK) {
					Company company = companyDialog.getCompany();
					if(company!=null){
						idText.setText(String.valueOf(company.getId()));
						nameText.setText(company.getName());
					}
				}
			}
		});
		
		new Label(payComposite, SWT.NONE);
		//公司moi id
		idText = new Text(payComposite, SWT.BORDER);
		idText.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false,
				1, 1));
		idText.setEditable(false);
		idText.setVisible(false);

		// 楼栋
		Label buildingLabel = new Label(payComposite, SWT.RIGHT);
		buildingLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		buildingLabel.setText("楼栋：");
		buildingComboViewer = new ComboViewer(payComposite, SWT.READ_ONLY);
		buildingCombo = buildingComboViewer.getCombo();
		buildingCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		buildingComboViewer.setContentProvider(ArrayContentProvider
				.getInstance());
		buildingComboViewer.setLabelProvider(new LabelProvider() {
			@Override
			public String getText(Object element) {
				if (element instanceof Building) {
					Building building = (Building) element;
					return building.getEms_name();
				}
				return super.getText(element);
			}
		});
		// 模拟数据
		// Building[] buildings = new Building[] { new Building(1, "楼1", "1"),
		// new Building(2, "楼2", "2"), new Building(3, "楼3", "3") };
		// 获取楼数据
		List<Building> buildings = EstateManager.getBuildings();
		buildingComboViewer.setInput(buildings);

		buildingComboViewer
				.addSelectionChangedListener(new ISelectionChangedListener() {
					@Override
					public void selectionChanged(SelectionChangedEvent event) {
						IStructuredSelection selection = (IStructuredSelection) event
								.getSelection();
						long buildingId = ((Building) selection
								.getFirstElement()).getId();
						// 获取楼层数据
						List<Floor> floors = EstateManager
								.getFloorsByBuilding(buildingId);
						floorComboViewer.setInput(floors);
					}
				});

		// 楼层
		Label floorLabel = new Label(payComposite, SWT.RIGHT);
		floorLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		floorLabel.setText("楼层：");
		floorComboViewer = new ComboViewer(payComposite, SWT.READ_ONLY);
		floorCombo = floorComboViewer.getCombo();
		floorCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		floorComboViewer.setContentProvider(ArrayContentProvider.getInstance());
		floorComboViewer.setLabelProvider(new LabelProvider() {
			@Override
			public String getText(Object element) {
				if (element instanceof Floor) {
					Floor floor = (Floor) element;
					return floor.getEms_name();
				}
				return super.getText(element);
			}
		});

		floorComboViewer
				.addSelectionChangedListener(new ISelectionChangedListener() {
					@Override
					public void selectionChanged(SelectionChangedEvent event) {
						IStructuredSelection selection = (IStructuredSelection) event
								.getSelection();
						long floorId = ((Floor) selection.getFirstElement())
								.getId();
						// 获取楼层数据
						List<Room> rooms = EstateManager
								.getRoomsByFloor(floorId);
						roomComboViewer.setInput(rooms);
					}
				});

		// 房间号
		Label roomLabel = new Label(payComposite, SWT.RIGHT);
		roomLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		roomLabel.setText("房间：");
		roomComboViewer = new ComboViewer(payComposite, SWT.READ_ONLY);
		roomCombo = roomComboViewer.getCombo();
		roomCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		roomComboViewer.setContentProvider(ArrayContentProvider.getInstance());
		roomComboViewer.setLabelProvider(new LabelProvider() {
			@Override
			public String getText(Object element) {
				if (element instanceof Room) {
					Room room = (Room) element;
					return room.getEms_name();
				}
				return super.getText(element);
			}
		});
		roomComboViewer
				.addSelectionChangedListener(new ISelectionChangedListener() {
					@Override
					public void selectionChanged(SelectionChangedEvent event) {
						IStructuredSelection selection = (IStructuredSelection) event
								.getSelection();
					}
				});

		// 结算项目
		Label itemLabel = new Label(payComposite, SWT.RIGHT);
		itemLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		itemLabel.setText("结算项目：");
		itemCombo = new Combo(payComposite, SWT.NONE);
		itemCombo.setItems(new String[] { "物业费", "动力电费", "照明电费" });
		itemCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		
		new Label(payComposite, SWT.NONE);
		// 查询按钮
		queryButton = new Button(payComposite, SWT.NONE);
		queryButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true,
				false, 1, 1));
		queryButton.setText("查询");
		queryButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				int state = Verify(idText.getText(), itemCombo.getText(),
						buildingCombo.getText(), floorCombo.getText(),
						roomCombo.getText());
				SeqGenereter.clearseq();
				switch (state) {
				case 0:
					MessageDialog.openWarning(parent.getShell(), "警告",
							"您输入的查询条件不合法");
					break;
				case 1:
					tableViewer.setInput(BillManager.getFalseBillLogsbyCompany_moi_id(4389L));
					break;
				case 2:
					tableViewer.setInput(BillManager.getAllBillLogs());
					break;
				default:
					break;
				}
			}
		});

		new Label(payComposite, SWT.NONE);
		// 交费按钮
		payButton = new Button(payComposite, SWT.NONE);
		payButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true,
				false, 1, 1));
		payButton.setText("交费");
		payButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				
			}
		});

		tableViewer = new TableViewer(parent, SWT.BORDER | SWT.FULL_SELECTION);
		table = tableViewer.getTable();
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

		TableViewerColumn seqViewerColumn = new TableViewerColumn(tableViewer,
				SWT.NONE);
		TableColumn seqTableColumn = seqViewerColumn.getColumn();
		seqTableColumn.setResizable(false);
		seqTableColumn.setAlignment(SWT.CENTER);
		seqTableColumn.setWidth(40);
		seqTableColumn.setText("序号");

		TableViewerColumn nameViewerColumn = new TableViewerColumn(tableViewer,
				SWT.NONE);
		TableColumn nameTableColumn = nameViewerColumn.getColumn();
		nameTableColumn.setAlignment(SWT.CENTER);
		nameTableColumn.setWidth(200);
		nameTableColumn.setText("公司名称");

		TableViewerColumn buildingViewerColumn = new TableViewerColumn(
				tableViewer, SWT.NONE);
		TableColumn buildingTableColumn = buildingViewerColumn.getColumn();
		buildingTableColumn.setResizable(false);
		buildingTableColumn.setAlignment(SWT.CENTER);
		buildingTableColumn.setWidth(50);
		buildingTableColumn.setText("楼栋");

		TableViewerColumn floorViewerColumn = new TableViewerColumn(
				tableViewer, SWT.NONE);
		TableColumn floorTableColumn = floorViewerColumn.getColumn();
		floorTableColumn.setResizable(false);
		floorTableColumn.setAlignment(SWT.CENTER);
		floorTableColumn.setWidth(50);
		floorTableColumn.setText("楼层");

		TableViewerColumn roomViewerColumn = new TableViewerColumn(tableViewer,
				SWT.NONE);
		TableColumn roomTableColumn = roomViewerColumn.getColumn();
		roomTableColumn.setResizable(false);
		roomTableColumn.setAlignment(SWT.CENTER);
		roomTableColumn.setWidth(50);
		roomTableColumn.setText("房间");

		TableViewerColumn timeViewerColumn = new TableViewerColumn(tableViewer,
				SWT.NONE);
		TableColumn timeTableColumn = timeViewerColumn.getColumn();
		timeTableColumn.setResizable(false);
		timeTableColumn.setAlignment(SWT.CENTER);
		timeTableColumn.setWidth(150);
		timeTableColumn.setText("结算时间");

		TableViewerColumn itemViewerColumn = new TableViewerColumn(tableViewer,
				SWT.NONE);
		TableColumn itemTableColumn = itemViewerColumn.getColumn();
		itemTableColumn.setResizable(false);
		itemTableColumn.setAlignment(SWT.CENTER);
		itemTableColumn.setWidth(60);
		itemTableColumn.setText("结算项目");

		TableViewerColumn beginAmountViewerColumn = new TableViewerColumn(
				tableViewer, SWT.NONE);
		TableColumn beginAmountTableColumn = beginAmountViewerColumn
				.getColumn();
		beginAmountTableColumn.setResizable(false);
		beginAmountTableColumn.setAlignment(SWT.CENTER);
		beginAmountTableColumn.setWidth(60);
		beginAmountTableColumn.setText("初始数");

		TableViewerColumn endAmountViewerColumn = new TableViewerColumn(
				tableViewer, SWT.NONE);
		TableColumn endAmountTableColumn = endAmountViewerColumn.getColumn();
		endAmountTableColumn.setResizable(false);
		endAmountTableColumn.setAlignment(SWT.CENTER);
		endAmountTableColumn.setWidth(60);
		endAmountTableColumn.setText("目前数");

		TableViewerColumn amountViewerColumn = new TableViewerColumn(
				tableViewer, SWT.NONE);
		TableColumn amountTableColumn = amountViewerColumn.getColumn();
		amountTableColumn.setResizable(false);
		amountTableColumn.setAlignment(SWT.CENTER);
		amountTableColumn.setWidth(60);
		amountTableColumn.setText("用量");

		TableViewerColumn moneyViewerColumn = new TableViewerColumn(
				tableViewer, SWT.NONE);
		TableColumn moneyTableColumn = moneyViewerColumn.getColumn();
		moneyTableColumn.setResizable(false);
		moneyTableColumn.setAlignment(SWT.CENTER);
		moneyTableColumn.setWidth(60);
		moneyTableColumn.setText("金额(元)");

		TableViewerColumn bealflagViewerColumn = new TableViewerColumn(
				tableViewer, SWT.NONE);
		TableColumn bealflagTableColumn = bealflagViewerColumn.getColumn();
		bealflagTableColumn.setResizable(false);
		bealflagTableColumn.setAlignment(SWT.CENTER);
		bealflagTableColumn.setWidth(80);
		bealflagTableColumn.setText("是否已付款");
		
		tableViewer.setContentProvider(new TableContentProvider());
		tableViewer.setLabelProvider(new TableLabelProvider());
	}

	// 验证查询消费记录的表单项，返回值的为int，几个值含义如下
	// 0:表单填写不合法。 1：填写了房间号，则按照房间号查找。2：没填写房间号，按公司编号查找。
	public int Verify(String companyCode, String itemType, String buildingId,
			String floorId, String roomId) {
		return 2;
		// TODO Auto-generated method stub
		/*if (itemType != null) {
			if (buildingId != null && floorId != null && roomId != null) {
				if (itemType != null) {
					return 1;
				} else {
					return 0;
				}
			} else if (companyCode != null) {
				return 2;
			} else {
				return 0;
			}
		} else {
			return 0;
		}*/
	}

	@PreDestroy
	public void preDestroy() {
		// TODO Your code here
	}
}